package com.cljz.makepassenger.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.present.activity.PaySetP;
import com.cljz.makepassenger.utils.CommonUtils;
import com.cljz.makepassenger.utils.T;
import com.cljz.makepassenger.utils.date.TextUtil;
import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxTextView;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.BindView;
import rx.functions.Action1;

public class PaySetActivity extends BaseActivity<PaySetP> {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_modify)
    TextView tvModify;
    @BindView(R.id.pay_weixin)
    LinearLayout llWeixin;
    @BindView(R.id.pay_alipay)
    LinearLayout llAlipay;
    @BindView(R.id.ll_main)
    LinearLayout llMain;
    private PopupWindow editorPayIdPopWindow;

    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("选择到账方式");
        String alipay = getP().getLocalUserInfo(getP().getToken()).getAlipay();
        if (!TextUtil.isEmpty(alipay)) {
            tvPhone.setText(" (" + alipay + ")");
            tvModify.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_pay_set;
    }

    @Override
    public PaySetP newP() {
        return new PaySetP();
    }

    @Override
    protected void initListener() {
        RxView.clicks(ivBack)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        finish();
                    }
                });

        RxView.clicks(llWeixin)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showWXLogin();
                    }
                });
        RxView.clicks(llAlipay)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        String alipay = getP().getLocalUserInfo(getP().getToken()).getAlipay();
                        String name = getP().getLocalUserInfo(getP().getToken()).getUser_name();
                        if (TextUtil.isEmpty(alipay) || TextUtil.isEmpty(name)) {
                            showEditorPayIdPopWindow();
                        } else {
                            setAlipayResult(alipay, name);
                        }
                    }
                });
        RxView.clicks(tvModify)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        showEditorPayIdPopWindow();
                    }
                });
    }

    public void setAlipaySuccess(String id, String name) {
        setAlipayResult(id, name);
    }

    public void setAlipayFailed(String error) {
        T.getInstance().showShort(error);
    }

    public void setAlipayResult(String id, String name) {
        Intent intent = new Intent();
        intent.putExtra("type", WithdrawActivity.ALIPAY);
        intent.putExtra("id", id);
        intent.putExtra("name", name);

        setResult(RESULT_OK, intent);
        finish();
    }

    private void showWXLogin() {
        UMShareAPI.get(this).getPlatformInfo(this, SHARE_MEDIA.WEIXIN, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                T.getInstance().showShort("授权成功");
                Intent intent = new Intent();
                intent.putExtra("type", WithdrawActivity.WEIXIN);
                intent.putExtra("unionId", map.get("unionid"));
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                T.getInstance().showShort("授权失败");
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {
                T.getInstance().showShort("授权取消");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    //popupWindow在界面的设置
    private void showEditorPayIdPopWindow() {
        View editorPayIdView = View.inflate(App.getInstance(), R.layout.pop_edit_paypal, null);
        FrameLayout flPayIdEditorClose = (FrameLayout) editorPayIdView.findViewById(R.id.fl_pay_id_editor_close);
        final EditText etPayIdEditor = (EditText) editorPayIdView.findViewById(R.id.et_pay_id_editor);
        final EditText etPayNameEditor = (EditText) editorPayIdView.findViewById(R.id.et_pay_name_editor);
        final Button btnPayIdSubmit = (Button) editorPayIdView.findViewById(R.id.btn_pay_id_submit);
        final ImageView ivWalletDelete = (ImageView) editorPayIdView.findViewById(R.id.iv_wallet_delete);
        final ImageView ivWalletNameDelete = (ImageView) editorPayIdView.findViewById(R.id.iv_wallet_name_delete);
        editorPayIdPopWindow = new PopupWindow(editorPayIdView,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                true);
        editorPayIdPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        editorPayIdPopWindow.setFocusable(true);
        editorPayIdPopWindow.setOutsideTouchable(true);
        editorPayIdPopWindow.setTouchable(true);
        editorPayIdPopWindow.update();
        editorPayIdPopWindow.showAtLocation(llMain, Gravity.CENTER, 0, 0);
        RxView.clicks(flPayIdEditorClose)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        editorPayIdPopWindow.dismiss();
                    }
                });
        RxView.clicks(btnPayIdSubmit)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        String id = etPayIdEditor.getText().toString();
                        String name = etPayNameEditor.getText().toString();
                        if (TextUtil.isEmpty(id)) {
                            T.getInstance().showShort("请输入支付宝账号");
                            return;
                        }
                        if (TextUtil.isEmpty(id)) {
                            T.getInstance().showShort("请输入真实姓名");
                            return;
                        }
                        getP().setAlipay(id, name);
                    }
                });
        RxView.clicks(ivWalletDelete)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        etPayIdEditor.setText("");
                    }
                });
        RxView.clicks(ivWalletNameDelete)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        etPayNameEditor.setText("");
                    }
                });
        RxTextView.textChanges(etPayIdEditor)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if (charSequence.length() == 0) {
                            ivWalletDelete.setVisibility(View.GONE);
                            btnPayIdSubmit.setEnabled(false);
                        } else {
                            ivWalletDelete.setVisibility(View.VISIBLE);
                            if (etPayNameEditor.getText().length() > 0 && (CommonUtils.checkEmail(charSequence.toString()) || CommonUtils.checkPhone(charSequence.toString()))) {
                                btnPayIdSubmit.setEnabled(true);
                            }
                        }
                    }
                });
        RxTextView.textChanges(etPayNameEditor)
                .compose(this.<CharSequence>bindToLifecycle())
                .subscribe(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        if (charSequence.length() == 0) {
                            ivWalletNameDelete.setVisibility(View.GONE);
                            btnPayIdSubmit.setEnabled(false);
                        } else {
                            ivWalletNameDelete.setVisibility(View.VISIBLE);
                            if (etPayIdEditor.getText().length() > 0) {
                                btnPayIdSubmit.setEnabled(true);
                            }
                        }
                    }
                });
    }

}
